<?php
class AuthController extends Controller
{
    public function actionLogin()
    {
		$errorMessage = null;
		if ($this->isPostRequest()) {
			$login = $this->getParam('login');
			$password = $this->getParam('password');
			$identity = new UserIdentity($login, $password);
			if ($identity->authenticate()) {
				$model = $identity->getState('model');
				if (User::ROLE_ADMIN == $model->role) {
					Yii::app()->user->login($identity);
					$this->redirect(array('/admin/index/index'));
				} else {
					$errorMessage = 'Доступ запрещен';
				}
			} else {
				$errorMessage = $identity->errorMessage;
			}
		}
		$this->render('login', array('errorMessage' => $errorMessage));
    }

	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->render('logout');
	}
} 
